<?php
class TbfUeditor {
    private static $ueditorConfig = <<<'EOF'
{
    "imageActionName": "uploadimage",
    "imageFieldName": "file",
    "imageMaxSize": 2048000,
    "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"],
    "imageCompressEnable": true,
    "imageCompressBorder": 1600,
    "imageInsertAlign": "none",
    "imageUrlPrefix": "",
    "imagePathFormat": "/ueditor/php/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}",

    "scrawlActionName": "uploadscrawl",
    "scrawlFieldName": "upfile",
    "scrawlPathFormat": "/ueditor/php/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}",
    "scrawlMaxSize": 2048000,
    "scrawlUrlPrefix": "",
    "scrawlInsertAlign": "none",

    "snapscreenActionName": "uploadimage",
    "snapscreenPathFormat": "/ueditor/php/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}",
    "snapscreenUrlPrefix": "",
    "snapscreenInsertAlign": "none",

    "catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"],
    "catcherActionName": "catchimage",
    "catcherFieldName": "source",
    "catcherPathFormat": "/ueditor/php/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}",
    "catcherUrlPrefix": "",
    "catcherMaxSize": 2048000,
    "catcherAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"],

    "videoActionName": "uploadvideo",
    "videoFieldName": "upfile",
    "videoPathFormat": "/ueditor/php/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}",
    "videoUrlPrefix": "",
    "videoMaxSize": 102400000,
    "videoAllowFiles": [
        ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
        ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"],

    "fileActionName": "uploadfile",
    "fileFieldName": "upfile",
    "filePathFormat": "/ueditor/php/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}",
    "fileUrlPrefix": "",
    "fileMaxSize": 51200000,
    "fileAllowFiles": [
        ".png", ".jpg", ".jpeg", ".gif", ".bmp",
        ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
        ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
        ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
        ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
    ],

    "imageManagerActionName": "listimage",
    "imageManagerListPath": "/ueditor/php/upload/image/",
    "imageManagerListSize": 20,
    "imageManagerUrlPrefix": "",
    "imageManagerInsertAlign": "none",
    "imageManagerAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"],

    "fileManagerActionName": "listfile",
    "fileManagerListPath": "/ueditor/php/upload/file/",
    "fileManagerUrlPrefix": "",
    "fileManagerListSize": 20,
    "fileManagerAllowFiles": [
        ".png", ".jpg", ".jpeg", ".gif", ".bmp",
        ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
        ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
        ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
        ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
    ]
}
EOF;

    static function UeditorInterface(){
        $action = TbfInput::request('action');
        $config = json_decode(self::$ueditorConfig,true);
        if ($action==='config'){
            echo json_encode($config);
            return;
        }elseif($action==='uploadimage'){
            self::UeditorImageUpload();
            return;
        }elseif ($action==='listimage'){
            self::UeditorListImage();
            return;
        }else{
            echo json_encode(array('err'=>'action not found'));
        }
    }
    private static function UeditorImageUpload(){
        $ret = TbfUpload::ArrayImageUpload();
        if (!empty($ret['err'])){
            echo json_encode(array('Status'=>$ret['err']));
            return;
        }
        echo json_encode(array(
            'state'=>'SUCCESS',
            'title'=>'1.jpg',
            'original'=>'1.jpg',
            'url'=>$ret['url'],
            'size'=>16778,
            'type'=>'.jpg',
        ));
        return;
    }
    private static function UeditorListImage(){
        $list = self::UeditorListImageInner();
        echo json_encode(array(
            "state" => "SUCCESS",
            "list" => $list,
            "start" => 0,
            "total" => count($list),
        ));
    }
    private static function UeditorListImageInner(){
        $list = array();
        $dir = TbfConfig::$UploadPath.'/image';
        if (!is_dir($dir)) {
            return array();
        }
        $dh = opendir($dir);
        if (empty($dh)){
            return array();
        }
        $i=0;
        while (($file = readdir($dh)) !== false) {
            if (strpos($file,'.')===0){
                continue;
            }
            $list[] = array('url'=>TbfConfig::$UploadUrlPrefix.'/image/'.$file,'mtime'=>0);
            if ($i>10){
                break;
            }
        }
        closedir($dh);
        return $list;
    }
}